require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

require(['jquery', 'doT', 'common', 'select2', 'bootstrap', 'paginator'], function($, doT, common) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var goodsList;
        var total;
        var pageSize = 10;

        //显示只有管理员可见的功能
        if (role == 1) {
            $('.managerOnly').removeClass('managerOnly');
        }

         function getSchoolList() {
            $.getJSON(common.urlBase + '/school/allSchool.json', function(result) {
                var schoolTmpl = doT.template($('#schoolTmpl').text());
                $('#schoolSelect').html(schoolTmpl(result.data));
                $('#schoolSelect').select2();
                getShopList();
            });
        }
        getSchoolList();

        $('#schoolSelect').on('change',getShopList);

        function getGoods(page, size) {
            var shopid=$('#shopSelect').val();
            var url = common.urlBase + '/item/query/shop/all.json?shop_id='+shopid+'&page_no=' + page + '&page_size=' + size;
            $.getJSON(url, function(result) {
                console.log(result);
                goodsList = result.data;
                var goodsTmpl = doT.template($('#goodsTmpl').text());
                $('#goodsList').html(goodsTmpl(goodsList.obj));
                total = goodsList.total_results;
                $('#total').text(total);
                var pageNumber = Math.ceil(total / pageSize);
                initPaginator(page, 8, pageNumber);
            });
        }

        function refreshGoodsList(page) {
            getGoods(page, pageSize);
        }

        //初始化分页组件
        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function(event, oldPage, newPage) {
                    refreshGoodsList(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }

        $(document).on('click', '.removeGoods', function(event) {
            var id = $(event.target).attr('data-id');
            $.post(common.urlBase + '/item/del/numiid/' + id + '.json', function(result) {
                if (result.ok) {
                    $(event.target).parent().parent().slideUp('slow');
                    total--;
                    $('#total').text(total);
                }
            }, 'json');
        });

        function getShopList() {
            var sid;
            if (sessionStorage.getItem('school_id')) {
                sid = sessionStorage.getItem('school_id');
            } else {
                sid = $('#schoolSelect').val();
            }
            $.getJSON(common.urlBase + '/shop/school-shop/' + sid + '.json', function(result) {
                if (result.ok) {
                    var shopTmpl = doT.template($('#shopTmpl').text());
                    $('#shopSelect').html(shopTmpl(result.data));
                    $('#shopSelect').select2();
                } else {
                    $('#noticeMsg').text('该学校还未添加饭堂，请先添加饭堂!');
                    $('#noticeModal').modal('show');
                    $('#shopSelect').html('');
                }
            });
        }

        $('.goodsQuery').on('click',function (event) {
            console.log('in');
            refreshGoodsList(1);
        });
    });

});
